package com.example.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@Service
public class TimeService {

    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
    @Async("taskExecutor")
    public void printCurrentTimeRepeatedly(int count) {
        System.out.println("开始执行时间输出任务，将输出 " + count + " 次时间");
        System.out.println("执行线程: " + Thread.currentThread().getName());
        
        for (int i = 0; i < count; i++) {
            try {
                Thread.sleep(1000);

                String currentTime = LocalTime.now().format(TIME_FORMATTER);

                System.out.println("[" + (i + 1) + "/" + count + "] 当前时间: " + currentTime + 
                                  " - 线程: " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println("时间输出任务被中断");
                break;
            }
        }
        
        System.out.println("时间输出任务完成");
    }

    @Async("taskExecutor")
    public void countdown(int seconds, CountDownLatch latch) {
        System.out.println("开始倒计时: " + seconds + "秒");
        System.out.println("执行线程: " + Thread.currentThread().getName());
        
        for (int i = seconds; i > 0; i--) {
            try {
                Thread.sleep(1000);
                
                System.out.println("倒计时: " + i + "秒 - 线程: " + Thread.currentThread().getName());
                
                if (i % 60 == 0) {
                    System.out.println("--- 剩余 " + (i / 60) + " 分钟 ---");
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println("倒计时任务被中断");
                break;
            }
        }
        
        System.out.println("倒计时结束!");

        if (latch != null) {
            latch.countDown();
        }
    }
    @Async("taskExecutor")
    public void countdown(int seconds) {
        countdown(seconds, null);
    }
    @Async("taskExecutor")
    public void simulateLongRunningTask(String taskName, int duration, CountDownLatch latch) {
        System.out.println("开始执行长时间任务: " + taskName);
        System.out.println("预计耗时: " + duration + "秒");
        System.out.println("执行线程: " + Thread.currentThread().getName());
        
        try {
            for (int i = 1; i <= duration; i++) {
                Thread.sleep(1000);
                System.out.println("任务 '" + taskName + "' 进度: " + i + "/" + duration + 
                                  " - 线程: " + Thread.currentThread().getName());
                if (i % 5 == 0) {
                    System.out.println("--- 任务 '" + taskName + "' 已完成 " + 
                                      (i * 100 / duration) + "% ---");
                }
            }
            
            System.out.println("任务 '" + taskName + "' 已完成!");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("任务 '" + taskName + "' 被中断");
        } finally {
            if (latch != null) {
                latch.countDown();
            }
        }
    }
}